﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using _4.Modelado.Seguridad;

namespace _3.Datos.Seguridad
{
   public class AreaBD
    {

       /// <summary>
       /// Este método inserta y modifica una área
       /// </summary>
       /// <param name="comando">sentencia SQL</param>
       /// <param name="obj">Contiene las variable de area</param>
       public void AreaGuardar(SqlCommand comando, Area obj)
       {
           try
           {
               string sp = "usp_AreaGuardar";

               comando.CommandType = CommandType.StoredProcedure;
               comando.CommandText = sp;
               comando.Parameters.Clear();

               comando.Parameters.Add(new SqlParameter("@PkArea", SqlDbType.Int));
               comando.Parameters.Add(new SqlParameter("@AreaDescripcion", SqlDbType.VarChar));


               comando.Parameters[0].Value = obj.iPKArea;
               comando.Parameters[1].Value = obj.sAreaDescripcion;
              // comando.Parameters[2].Direction = ParameterDirection.Output;


               comando.ExecuteNonQuery();


           }
           catch (Exception exc)
           {

               throw exc;
           }
       }


       public List<Area> AreasConsultar(SqlCommand comando)
        {
            try
            {
                string sp = "usp_AreasConsultar";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                IDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                var ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                var lista = new List<Area>();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var obj = new Area();
                    obj.iPKArea = Convert.ToInt32(dt.Rows[i]["PKArea"]);
                    obj.sAreaDescripcion = dt.Rows[i]["AreaDescripcion"].ToString();
               
                    lista.Add(obj);
                }
                return lista;
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }
     
       /// <summary>
       /// Llena un data table para poder 
       /// llenar el combo area
       /// </summary>
       /// <param name="comando"></param>
       /// <returns>DataTable Area</returns>
       
       public DataTable LlenarComboArea(SqlCommand comando)
        {
            try
            {
                string sp = "usp_AreasConsultar";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                IDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                var ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];


                return ds.Tables[0];
            }
            catch (Exception exc)
            {

                throw exc;
            }
        }

        public void AreaEliminar(SqlCommand comando, Area obj)
        {
            try
            {
                string sp = "usp_AreaEliminar";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@PkArea", SqlDbType.Int));
                comando.Parameters[0].Value = obj.iPKArea;


                comando.ExecuteNonQuery();

            }
            catch (Exception e)
            {

                throw e;
            }
        }
    }
}
